//
//  HomeLikeCell.m
//  智慧小区
//
//  Created by lab603 on 15/6/29.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "HomeLikeCell.h"
#import "HomeAdView.h"
#define padding 10
#define cellBorder 15
@interface HomeLikeCell()
//按钮
@property (nonatomic,weak)UIButton *titleBtn;
//分隔线
@property (nonatomic,weak)UIImageView *separator;
//广告视图
@property (nonatomic,weak)HomeAdView *adView;
@end
@implementation HomeLikeCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"HomeLikeCell";
    //HomeSudokuCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //if (cell==nil) {
    HomeLikeCell *cell = [[HomeLikeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    cell.accessoryType = UITableViewCellAccessoryNone;
    //}
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectedBackgroundView = [[UIView alloc] init];
        //按钮
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:@"猜你喜欢" forState:UIControlStateNormal];
        [btn setTitleColor:APP_THEME_COLOR forState:UIControlStateNormal];
        [btn setImage:[UIImage imageWithName:@"more_button_blue"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        btn.userInteractionEnabled = YES;
        [btn setBackgroundImage:[UIImage resizedImageWithName:@"timeline_card_middlebottom_highlighted"] forState:UIControlStateHighlighted];
        [self.contentView addSubview:btn];
        self.titleBtn = btn;
        //分割线
        UIImageView *separator = [[UIImageView alloc] init];
        separator.backgroundColor = APP_BACK_COLOR;
        [self.contentView addSubview:separator];
        self.separator = separator;
        //广告列表
        HomeAdView *adView = [[HomeAdView alloc] init];
        [self.contentView addSubview:adView];
        self.adView = adView;
    }
    return  self;
}
- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    //设置各控件的frame
    //背景frame
    CGFloat backgroundX = 0;
    CGFloat backgroundY = 0;
    CGFloat backgroundW = [UIScreen mainScreen].bounds.size.width;
    //根据最后的尺寸计算
    CGFloat backgroundH = 0;
    //按钮frame
    CGFloat titleBtnX = 0;
    CGFloat titleBtnY = 0;
    CGFloat titleBtnW = 100;
    CGFloat titleBtnH = 35;
    _titleBtn.frame = CGRectMake(titleBtnX, titleBtnY, titleBtnW, titleBtnH);
    //分割线frame
    CGFloat separatorX = 0;
    CGFloat separatorY = CGRectGetMaxY(_titleBtn.frame);
    CGFloat separatorW = backgroundW;
    CGFloat separatorH = 1;
    _separator.frame = CGRectMake(separatorX, separatorY, separatorW, separatorH);
    //广告列表
    CGFloat adViewX = 0;
    CGFloat adViewY = CGRectGetMaxY(_separator.frame)+1;
    CGSize adViewSize = [HomeAdView sizeWithDataArray:dataArray];
    _adView.frame = (CGRect){{adViewX, adViewY},adViewSize};
    backgroundH = CGRectGetMaxY(_adView.frame)+ padding;
    //得到高度
    self.backgroundView.frame = CGRectMake(backgroundX, backgroundY, backgroundW, backgroundH);
    //_cellHeight = backgroundH;
    
    //传值
    self.adView.adModel = dataArray;
}
- (void)setFrame:(CGRect)frame
{
    frame.origin.y += cellBorder;
    frame.size.height -= cellBorder;
    [super setFrame:frame];
}
@end
